<?php
class Admin_ArticleController extends My_Controllers_Admin {
	public function indexAction() {
		$this->view->b = $this->getSelect ();
		Zend_Debug::dump ( $this->view->b, 'getSelect', false );
		
		$id = $this->_request->getParam ( 'id' );
		if ($id != "") {
			$news = new Article ( );
			$this->view->newsDb = $news->find ( $id )->current ();
			Zend_Debug::dump ( $this->view->newsDb->toArray (), '$newsDb', false );
			$user = new User ( );
			$this->view->username = $user->find ( $this->view->newsDb->news_uid )->current ()->username;
			$this->view->action = "save";
		} else {
			$this->view->username = $this->username;
			$this->view->action = "insert";
		}
	}
	private function getSelect() {
		$db = Zend_Registry::get ( "db" );
		$select = $db->select ();
		$select->from ( array ('k' => 'bc_keshi' ), array ('kename' ) );
		$select->joinRight ( array ('l' => 'bc_lanmu' ), 'k.id = l.kid', array ('lname' => 'name', 'id' ) );
		$select->order ( 'k.id' );
		if ($this->group != 'admin') {
			$select->where ( 'kid = ?', $this->ke_id );
		}
		$sql = $select->__toString ();
		$a = $db->fetchAll ( $sql );
		foreach ( $a as $v ) {
			$b [$v ['kename']] [$v ['id']] = $v ['lname'];
		}
		return $b;
	}
	public function insertAction() {
		$front = Zend_Controller_Front::getInstance ();
		$front->setParam ( 'noViewRenderer', true );
		$articleDate = $this->_request->getPost ();
		$auth = Zend_Auth::getInstance ();
		$articleDate ["news_uid"] = $auth->getIdentity ()->id;
		$articleDate ["time"] = time ();
		$articleDate ["news_content"] = parent::stripslashes_deep ( $articleDate ["news_content"] );
		$articleDate ["news_view"] = 0;
		unset ( $articleDate [123] );
		unset ( $articleDate ['id'] );
		$lanmu = new Lanmu ( );
		$where = $lanmu->getAdapter ()->quoteInto ( 'id = ?', $articleDate ["column_id"] );
		
		$articleDate ["section_id"] = $lanmu->fetchRow ( $where )->kid;
		
		Zend_Debug::dump ( $articleDate, "articleDate", false );
		
		$article = new Article ( );
		
		if ($this->group == 'admin') {
			$message = "发布成功!";
			$articleDate ["mode"] = 1;
		} elseif ($this->group == 'member') {
			$message = "文章发布成功.";
			$articleDate ["mode"] = 0;
		}
		
		try {
			for($i = 0; $i < 100; $i ++) {
				$article->insert ( $articleDate );
			}
			$this->_redirect ( '/admin/article/list/mod/0' );
			//			$this->_forward ( "message", "index", null, array ("message" => $message ) );
		} catch ( PDOException $e ) {
			echo "Exception caught : {$e->getMessage()}\n";
		}
	}
	public function saveAction() {
		$front = Zend_Controller_Front::getInstance ();
		$front->setParam ( 'noViewRenderer', true );
		Zend_Debug::dump ( $_FILES, '$_FILES', false );
		$articleDate = $this->_request->getPost ();
		
		if ($_FILES ['pic'] ['name'] != '') {
			$pic_name = time ();
			$ext = pathinfo ( $_FILES ['pic'] ['name'], PATHINFO_EXTENSION );
			$img = & My_Helper_Image::createFromFile ( $_FILES ['pic'] ['tmp_name'], $ext );
			$img->resize ( 75, 60 );
			$img->saveAsPng ( "./html/upfile/article/$pic_name.png" );
			$articleDate ['pic'] = $pic_name . ".png";
		}
		
		Zend_Debug::dump ( $articleDate, '$articleDate', true );
		
		unset ( $articleDate [123] );
		$lanmu = new Lanmu ( );
		$where = $lanmu->getAdapter ()->quoteInto ( 'id = ?', $articleDate ["column_id"] );
		
		$articleDate ["section_id"] = $lanmu->fetchRow ( $where )->kid;
		$articleDate ["news_content"] = parent::stripslashes_deep ( $articleDate ["news_content"] );
		
		Zend_Debug::dump ( $articleDate, "articleDate", true );
		
		$article = new Article ( );
		
		try {
			$where = $article->getAdapter ()->quoteInto ( 'id = ?', $articleDate ["id"] );
			$id = $article->update ( $articleDate, $where );
			$this->_redirect ( "/admin/article/list" );
			echo $id;
		} catch ( PDOException $e ) {
			echo "Exception caught : {$e->getMessage()}\n";
		}
	}
	public function listAction() {
		$this->_helper->layout->setLayout ( 'list' );
		$this->view->setHelperPath ( './application/views/helpers', 'My_View_Helper' );
		$this->view->mod = $this->_getParam ( "mod" );
	}
	public function jsonAction() {
		$this->view->setHelperPath ( './application/views/helpers', 'My_View_Helper' );
		
		$db = Zend_Registry::get ( 'db' );
		$select = $db->select ();
		$select2 = $db->select ();
		$this_page = $this->_getParam ( "page" );
		$count = 15;
		
		$select->from ( array ('n' => 'bc_news' ), array ('id', 'news_title', 'time', 'pic', 'mode' ) )->joinLeft ( array ('l' => 'bc_lanmu' ), 'n.column_id = l.id', array ('lname' => 'name' ) )->joinLeft ( array ('k' => 'bc_keshi' ), 'l.kid = k.id', array ('kename' ) )->joinLeft ( array ('u' => 'bc_users' ), 'n.news_uid = u.id', array ('username' ) );
		$select2->from ( array ('n' => 'bc_news' ), array ('count(*)' ) );
		if ($this->group != 'admin') {
			$select->where ( 'section_id = ?', $this->ke_id );
			$select2->where ( 'section_id = ?', $this->ke_id );
		}
		
		if ($this->_getParam ( "mod" ) != '') {
			$mode = $this->_getParam ( "mod" );
		} else {
			$mode = '0';
		}
		$this->view->mode = $mode;
		
		$acl = new My_acl ( );
		$this->view->contr = $acl->isAllowed ( $this->group, 'a' . $mode, 'control' );
		
		if ($mode == '0') {
			$select2->where ( 'mode >= ?', $mode );
			$select->where ( 'mode >= ?', $mode );
		} else {
			$select2->where ( 'mode = ?', $mode );
			$select->where ( 'mode = ?', $mode );
		}
		
		//		$select->limitPage ( $this_page + 1, $count );
		$select->order ( "id desc" )->order ( "time desc" )->order ( "mode desc" );
		$sql = $select->__toString ();
		
		Zend_Debug::dump ( $sql, '$sql', false );
		$data ['recordsReturned'] = 25;
		$data ["totalRecords"] = $db->fetchOne ( $select2 );
		$data ["startIndex"] = 0;
		$data ["sort"] = null;
		$data ["dir"] = "asc";
		$data ['news'] = $db->fetchAll ( $sql );
		foreach ( $data ['news'] as $k => $v ) {
			$data ['news'] [$k] ['time'] = date ( "Y年m月d日", $v ['time'] );
		}
		$this->view->data = $data;
		//		Zend_Debug::dump ( $this->view->data, '$news_list', true );
		$this->view->operate = array ('-1' => '回收站' );
	}
	public function getOneAction() {
		$front = Zend_Controller_Front::getInstance ();
		$front->setParam ( 'noViewRenderer', true );
		$layout = Zend_Layout::getMvcInstance ();
		if ($layout instanceof Zend_Layout) {
			$layout->disableLayout ();
		}
		$id = $this->_request->getParam ( 'id', 0 );
		$news = new Article ( );
		$newsDb = $news->find ( $id )->current ();
		echo $newsDb->news_content;
	}
	public function operateAction() {
		$front = Zend_Controller_Front::getInstance ();
		$front->setParam ( 'noViewRenderer', true );
		
		$id = $this->getRequest ()->getParam ( 'id' );
		if (is_string ( $id )) {
			$id = array ($id );
		}
		Zend_Debug::dump ( $id, '$id', false );
		
		if (is_array ( $id )) {
			$n = new Article ( );
			
			try {
				$data = array ('mode' => '-1' );
				for($i = 0; $i < count ( $id ); $i ++) {
					$where = $n->getAdapter ()->quoteInto ( 'id = ?', $id [$i] );
					Zend_Debug::dump ( $where, '$where', false );
					$result = $n->update ( $data, $where );
					echo $result;
				}
			} catch ( PDOException $e ) {
				echo "Exception caught : {$e->getMessage()}\n";
			}
		
		}
	}
	public function auditAction() {
		$front = Zend_Controller_Front::getInstance ();
		$front->setParam ( 'noViewRenderer', true );
		
		$a = new Article ( );
		$news = $a->find ( $this->_getParam ( 'id' ) );
		$news->current ()->mode = 1;
		$news->current ()->save ();
	}
}
